package com.hunan.yllxy.config.security;

import com.hunan.yllxy.exceptions.CustomerException;
import com.hunan.yllxy.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 刘华
 * @Date: 2024/3/28 0028 14:15
 * @Version 1.0
 */
@Slf4j
@Component
public class ValidateCodeFilter extends OncePerRequestFilter {

    @Autowired
    @Qualifier("handlerExceptionResolver")
    private HandlerExceptionResolver resolver;
//    @Autowired
//    RedisTemplate redisTemplate;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        log.info("请求地址：{}", requestURI);
        if (!"/login".equalsIgnoreCase(requestURI)) {
            filterChain.doFilter(request, response);
        } else {
            try {

                filterChain.doFilter(request, response);

            } catch (AuthenticationException e) {
                CustomerException customerException = new CustomerException(e.getMessage());
                resolver.resolveException(request, response, null, customerException);
            }
        }
    }
}
